package com.example.version.controll.util;

import com.example.version.controll.eo.TypeEO;
import com.example.version.controll.model.RepositoryConfig;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.util.List;

public class BatUtil {

    public static void runBat(TypeEO typeEO) throws Exception {
        if (FileUtil.configDTO == null) {
            FileUtil.initConfig();
        }
        List<RepositoryConfig> repositoryList = FileUtil.configDTO.getRepositoryList();
        if (CollectionUtils.isNotEmpty(repositoryList)) {
            for (RepositoryConfig repositoryConfig : repositoryList) {
                runBat(typeEO, repositoryConfig);
            }
        }
    }
    public static void runBat(TypeEO typeEO, RepositoryConfig repositoryConfig) throws Exception {
        runBat(typeEO, repositoryConfig, "");
    }

    public static void runBat(TypeEO typeEO, RepositoryConfig repositoryConfig, String prefix) throws Exception {
        if (StringUtils.hasText(prefix)) {
            prefix = prefix + "-";
        }
        File file1 = new File(PathManagerUtil.batPath + repositoryConfig.getName());
        // 如果脚本不存在则创建
        if (!file1.exists()) {
            BatUtil.generateBat(repositoryConfig, true);
        }
        File file = new File(PathManagerUtil.gitDataPath + repositoryConfig.getName());
        if (typeEO == TypeEO.CLONE) {
            // clone时已存在 则执行更新
            if (file.exists()) {
                BatUtil.run(PathManagerUtil.batPath + repositoryConfig.getName(), prefix + TypeEO.UPDATE.getName());
                return;
            }
        } else if (typeEO == TypeEO.UPDATE && !file.exists()) {
            runBat(TypeEO.CLONE, repositoryConfig, prefix);
            return;
        } else {
            if (!file.exists()) {
                throw new Exception("请先克隆仓库");
            }
        }
        BatUtil.run(PathManagerUtil.batPath + repositoryConfig.getName(), prefix + typeEO.getName());
    }

    public static void generateBat(RepositoryConfig repositoryConfig, boolean create) {
        if (!repositoryConfig.isOpen()) {
            return;
        }
        String fileName = PathManagerUtil.batPath + repositoryConfig.getName() + "\\";
        File file = new File(fileName);
        if (!file.exists()) {
            file.mkdir();
        }

        String cloneBat = "@echo off \n" +
                "cd " + PathManagerUtil.gitDataPath + " \n" +
                "git clone " + repositoryConfig.getGitUrl() + " \n" +
                "cd " + PathManagerUtil.gitDataPath + repositoryConfig.getName() + " \n" +
                "git checkout " + repositoryConfig.getBranch();
        FileUtil.exitCreateOrUpdate(fileName + TypeEO.CLONE.getName(), cloneBat, create);

        String commintBat = "@echo off \n" +
                "cd " + PathManagerUtil.gitDataPath + repositoryConfig.getName() + " \n" +
                "git checkout " + repositoryConfig.getBranch() + " \n" +
                "git pull \n" +
                "git add -u \n" +
                "git commit -m 'update-bom-version-by-util' \n" +
                "git push";
        FileUtil.exitCreateOrUpdate(fileName + TypeEO.COMMIT.getName(), commintBat, create);

        String updateLocalBat = "@echo off \n" +
                "cd " + PathManagerUtil.gitDataPath + repositoryConfig.getName() + " \n" +
                "git reset --hard \n" +
                "git checkout " + repositoryConfig.getBranch() + " \n" +
                "git pull ";
        FileUtil.exitCreateOrUpdate(fileName + TypeEO.UPDATE.getName(), updateLocalBat, create);
    }

    public static void run(String path, String fileName){
        try {
            System.out.println("开始执行脚本" + path + " " + fileName);
            boolean b = CommandUtil.executeCommand("cmd /c cd " + path + " && " + fileName);
            System.out.println("执行脚本结束" + path + " " + fileName);
        } catch (Exception e) {
            System.out.println("执行脚本发生异常" + path + " " + fileName + "  " + e.getMessage());
        }
    }
}
